/*
 * @Descripttion:
 * @Author: YuZhenJing
 * @Date: 2020-01-16 17:31:11
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-03-20 15:34:50
 */

/**
 * 查询角色类别数据集
 */
export function searchRoleTypeOptions() {
  const options = this.$html.get('roles/roleTypeOptions')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.roleTypeOptions = this.$objTools.dicToArray(data.returnData)
      }
    })
    .catch(() => {
      console.log('error:searchRoleTypeOptions')
    })
}

/**
 * 查询用户列表信息
 */
export function request() {
  this.tableDataParam.loading = true
  let param = {
    pageSize: this.tableDataParam.paginationData.pageSize,
    pageIndex: this.tableDataParam.paginationData.currentPage,
    filter: this.searchParams,
    sortBy: this.tableDataParam.paginationData.sortItem.prop,
    descending:
      this.tableDataParam.paginationData.sortItem.order == 'descending'
  }
  const options = this.$html.get('roles/pagedlist', param)
  options
    .then(res => {
      let data = res.returnData
      this.tableDataParam.data = data.list
      this.tableDataParam.paginationData.total = data.total
      this.tableDataParam.loading = false
    })
    .catch(() => {
      this.tableDataParam.loading = false
      console.log('error:user/request')
    })
}

/**
 * 编辑角色
 */
export function roleEdit(rowData) {
  this.$refs.roleEditRef.show(rowData)
}

/**
 * 删除用户
 */
export function singleDel(rowObj) {
  this.$confirm('确认要删除【' + rowObj.roleName + '】吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      this.doDel([rowObj.roleId])
    })
    .catch(() => {
      this.$notify.info({
        title: '消息',
        message: '已取消删除'
      })
    })
}

/**
 * 批量删除
 */
export function batchDel() {
  let ids = []
  this.tableDataParam.selectedData.map(item => {
    ids.push(item.roleId)
  })
  if (ids.length > 0) {
    this.$confirm(
      '您选择了【' + ids.length + '】角色信息，确认要删除吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
      .then(() => {
        this.doDel(ids)
      })
      .catch(() => {
        this.$notify.info({
          title: '消息',
          message: '已取消删除'
        })
      })
  } else {
    this.$alert('请至少选择一条用户信息进行删除', '提示', {
      type: 'warning',
      confirmButtonText: '确定'
    })
  }
}

/**
 * 执行删除
 */
export function doDel(param) {
  const delResult = this.$html.delete('roles/delRole', param)
  delResult
    .then(data => {
      if (data.returnType == 'success') {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
        this.request()
      }
    })
    .catch(() => {
      console.log('error:role/doDel')
    })
}

/**
 * 角色关联功能清单
 */
export function roleJoinMenu(rowObj) {
  this.$refs.roleJoinMenuRef.show(rowObj.roleId)
}

/**
 * 获取可选择的所有执行类角色
 *  使用此函数包含以下功能
 *    1.用户角色设置
 *    2.用户分组角色设置
 */
export function getRoleByExecute(callback) {
  const options = this.$html.get('roles/roleByExecute')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.roleData = []
        data.returnData.map(item => {
          this.roleData.push({
            key: item.roleId,
            label: item.roleName
          })
        })
        if (typeof callback == 'function') {
          callback()
        }
      }
    })
    .catch(error => {
      console.log('error:role/getRoleByExecute', error)
    })
}
/**
 * 获取可选择的所有管理类角色
 */
export function getRoleByManager(callback) {
  const options = this.$html.get('roles/roleByManager')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.roleData = []
        data.returnData.map(item => {
          this.roleData.push({
            key: item.roleId,
            label: item.roleName
          })
        })
        if (typeof callback == 'function') {
          callback()
        }
      }
    })
    .catch(() => {
      console.log('error:role/getRoleByManager')
    })
}
